var ctx = $("#ctx").val();
$(function () {
    $(".captcha").trigger("click");
    $(".register-button").click(function () {
        var inputs = $("#registerForm input");
        var register = true;
        for (var i = 1; i < inputs.length; i++) {
            if (inputs.eq(i).val() == "") {
                register = false;
            }
        }
        if (register == false) {
            mdui.snackbar({
                message: "各项不为空",
                position: "top"
            });
        } else if ($("#registerForm input[name=password]").val() != $("#registerForm input[name=password2]").val()) {
            mdui.snackbar({
                message: "两次密码不一致",
                position: "top"
            });
        } else {
            $.ajax({
                url: ctx + "/user/register",
                type: "POST",
                data: $("#registerForm").serialize(),
                async: false,
                success: function (data) {
                    if (data.code == 0) {
                        mdui.snackbar({
                            message: "注册成功！",
                            position: "top"
                        });
                        setTimeout(function () {
                            location.href = ctx + "/login";
                        })
                    } else {
                        mdui.snackbar({
                            message: data.msg,
                            position: "top"
                        });

                    }
                }
            });
        }
        setTimeout(function () {
            $(".captcha").trigger("click");
        }, 500)
    });
});
$(".captcha").click(function () {
    $(this).prop("src", ctx + "/captcha?date=" + new Date());
});
